<?php

namespace App\Http\Controllers\User;

use App\Http\Requests\RoleCreateRequest;
use App\Http\Requests\RoleUpdateRequest;
use App\Models\Permission;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Role;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        if($request->ajax()){
            $data = Role::paginate($request->get('limit', 30))->toArray();
            $result = [
                'code' => 0,
                'msg' => '正在请求中...',
                'count' => $data['total'],
                'data' => $data['data']
            ];
            return response()->json($result);
        }
        return view('user.role.index');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('user.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(RoleCreateRequest $request)
    {
        $data = $request->only(['name','display_name']);
        //role permission 创建都需要传入name参数
        if (Role::create($data)){
            return redirect()->to(route('admin.role'))->with(['status'=>'添加角色成功']);
        }
        return redirect()->to(route('admin.role'))->withErrors('系统错误');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {
        $role = Role::findOrFail($id);
        return view('role.edit',compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(RoleUpdateRequest $request, $id)
    {
        $role = Role::findOrFail($id);
        $data = $request->only(['name','display_name']);
        if ($role->update($data)){
            return redirect()->to(route('admin.role'))->with(['status'=>'更新角色成功']);
        }
        return redirect()->to(route('admin.role'))->withErrors('系统错误');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        if (Role::destroy($ids)){
            return response()->json(['code'=>0,'msg'=>'删除成功']);
        }
        return response()->json(['code'=>1,'msg'=>'删除失败']);
    }

    /**
     * 角色分配权限
     */
    public function permission(Request $request,$id)
    {
        $role = Role::find($id);
        $data = array_column($role->permissions->toArray(),'id');
        $permissions = (new Permission())->treeData($data);
        return view('user.role.permission',compact('role','permissions'));
    }

    /**
     * 存储权限
     */
    public function assignPermission(Request $request,$id)
    {
        $role = Role::find($id);
        $permissions = $request->get('permission_ids');
        if (empty($permissions)){
            $role->permissions()->detach();
            return response()->json(['code'=>0,'msg'=>'清除角色权限成功']);
        }
        $role->syncPermissions($permissions);
        return response()->json(['code'=>0,'msg'=>'更新角色权限成功']);
    }

    public function demo()
    {
        //将一个权限分配给角色
        $permission = [];
        $role = new Role();
        $role->givePermissionTo($permission);//方式一

        $role = [];
        $permission = new Permission();
        $permission->assignRole($role);//方式二

        //将多个权限分配给角色
        $permissions = [];
        $role->syncPermissions($permissions);//方式一
        $roles = [];
        $permission->syncRoles($roles);//方式二

        //通过角色删除权限
        $role->revokePermissionTo($permission);
        $permission->removeRole($role);
    }
}
